<?php
    use User\Model\LpicModel;
?>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading">
                图片详情
            </header>
            <div class="panel-body">
                <form class="form-horizontal" method="post"
                      action="/User/Lpic/picUpdate">
                    <input type="hidden" value="<?php echo I('get.liveId', '', '');?>" name="live_id" id="live_id">
                    <input type="hidden" value="<?php $val = (!empty($pic)) ?
                            $pic['pic_id'] : ''; echo $val;?>" name="picId" id="picId">
                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label">菜单图标:</label>

                        <div class="col-sm-10">
                            <img src="<?php
                        if(empty($pic) || empty($pic['pic_url_min'])){
                            echo '';
                        }else{
                            echo $pic['pic_url_min'];
                        }
                            ?>" alt="" id="pic_url_url" style="width: 200px;height: 200px;">
                            <input name="pic_url" id="pic_url" type="file">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">图片描述:</label>

                        <div class="col-lg-4">
                            <textarea id="describe" class="form-control" style="width:300px;height:100px;"
                                      name="describe"><?php echo $pic['describe']?></textarea>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">作者原名:</label>

                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="author" value="<?php $val=(!empty($pic)) ?
                            $pic['author'] : ''; echo $val;?>" id="author">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">昵称:</label>

                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="nickname" value="<?php $val=(!empty($pic)) ?
                            $pic['nickname'] : ''; echo $val;?>" id="nickname">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">审核状态:</label>

                        <div class="col-lg-4">
                            <select name="status" id="status" class="form-control">
                                <?php
                                foreach(LpicModel::getStatus() as $key=>$v_status){
                                echo '<option value="' . $key .'"';
                                if($key == $pic['status']){
                                echo ' selected';
                                }
                                echo '>' . $v_status . '</option>';
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group" style="<?php
                                if($pic['status'] == 2){
                                    echo 'display:block';
                                }else{
                                    echo 'display:none';
                                }
                            ?>" id="result">
                        <label class="col-sm-2 col-sm-2 control-label need">不通过原因:</label>

                        <div class="col-lg-4">
                            <input type="text" class="form-control" name="result" value="<?php echo $pic['result'];?>">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-2 col-sm-2 control-label need">归属专题:</label>

                        <div class="col-lg-4">
                            <select name="item_id" id="item_id" class="form-control" value="<?php echo $pic['item_id'];?>">
                                <option value="0" <?php
                                if($pic['item_id'] == 0){
                                    echo " selected";
                                }
                                ?>>--无--</option>
                                <?php
                                foreach($item_list as $key=>$v_status){
                                        echo '<option value="' . $v_status['item_id'] .'"';
                                        if($v_status['item_id'] == $pic['item_id']){
                                            echo ' selected';
                                        }
                                        echo '>' . $v_status['name'] . '</option>';
                                    }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-lg-offset-2 col-lg-10">
                            <button class="btn btn-primary" type="submit" id="submit">提交</button>
                            <button class="btn btn-default" type="button" onclick="history.back(-1);">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </section>
    </div>
</div>
<script>

    //--图片处理--
    var pic_url_data = '';
    $(function () {
        var pic_url = document.getElementById("pic_url");
        pic_url.addEventListener('change', readFile, false);
    });
    function readFile() {
        var file = this.files[0];
        if (!/image\/\w+/.test(file.type)) {
            alert("请确保文件为图像类型");
            return false;
        }
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
            $("#pic_url_url").attr("src", this.result);
            pic_url_data = this.result;
        }
    }

    $(function () {
        var status = document.getElementById("status");
        status.addEventListener('change', changeStatus, false);
    });
    function changeStatus() {
        var status_set = $(this).val();
        if(status_set == 2){
            //---拒绝--
            $('#result').css("display", "block");
        }else{
            $('#result').css("display", "none");
        }
    }

    $("#submit").click(function () {
        var data = $.param({"pic_url": pic_url_data}) + "&" + $("form").serialize();
        var url = $("form").attr("action");
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: data,
            url: url,
            async:false,
            success: function (res) {
                if (res.code == 1) {
                    alert("提交成功");
                    window.location = '/User/Lpic/pic?id=' + res.live_id;
                } else {
                    alert(res.msg);
                }
            }
        });
        return false;
    });
</script>